package com.potevio.simulator.common;


import com.potevio.simulator.config.MsgTypeConst;
import com.potevio.simulator.heart.HeartbeatClient;
import com.potevio.simulator.init.ApplicationStartQuartzJobListener;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 获取16进制报文
 */
public class GetHexMessage {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 起始域
     * @return
     */
    public String getStartingDomain(){
        return "FAF5";
    }

    /**
     * 获取数据域
     * @param classPath
     * @return
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     */
    public Map<String, LinkedHashMap<String, String>> getDataDomain(String classPath) throws ClassNotFoundException, IllegalAccessException {
        if(classPath.equals("com.potevio.simulator.entity.ChargeModelChargeMsg0x3F")){
            System.out.println("--------------");
        }
        Map<String, LinkedHashMap<String, String>> listMap = new HashMap<String, LinkedHashMap<String, String>>();
        // 循环获取所有变量的值
        Class clazz = Class.forName(classPath);
        Field[] fields = clazz.getFields();
        String commandCode = "";
        List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
        LinkedHashMap<String, String> mapAll = new LinkedHashMap<String, String>();
        LinkedHashMap<String, MsgBean> msgBeanMap = new LinkedHashMap<String, MsgBean>();
        // 遍历yml中配置好的报文字段
        for(Field field : fields ){
            // 命令代码
            if("commandCode".equals(field.getName())){
                commandCode = field.get(clazz).toString();
                // 通过命令代码，从全局报文配置变量中取出对应字段的Map集合
                msgBeanMap = ApplicationStartQuartzJobListener.chm.get(commandCode);
            }
            // 字段名称
            String fieldName = field.getName();
            // 字段值
            Object o = field.get(clazz);
            String fieldValue = field.get(clazz).toString();
            Map<String, String> map = new HashMap<String, String>();
            // 字段属性实体类
            MsgBean msgBean = msgBeanMap.get(fieldName);
            // 如果字段值为空，则从返回的报文中获取对应的字段值
            if(StringUtils.isBlank(fieldValue) && "time".equals(msgBean.getType())){
                fieldValue = sdf.format(new Date());
            }
            map.put(fieldName, fieldValue);
            mapAll.put(fieldName, fieldValue);
            mapList.add(map);
        }
        listMap.put(commandCode, mapAll);
        if("0x7A".equals(commandCode) || "0x10".equals(commandCode) || "0x70".equals(commandCode) || HeartbeatClient.chargingPileState == 1){
            MsgTypeConst.chm_Map.put(commandCode, mapAll);
        }
        return listMap;
    }

    /**
     * 获取数据域  3.4a
     * @param classPath
     * @return
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     */
    public Map<String, LinkedHashMap<String, String>> getDataDomain(String classPath, String chargePort) throws ClassNotFoundException, IllegalAccessException {
        Map<String, LinkedHashMap<String, String>> listMap = new HashMap<String, LinkedHashMap<String, String>>();
        // 循环获取所有变量的值
        Class clazz = Class.forName(classPath);
        Field[] fields = clazz.getFields();
        String commandCode = "";
        LinkedHashMap<String, String> mapAll = new LinkedHashMap<String, String>();
        LinkedHashMap<String, MsgBean> msgBeanMap = new LinkedHashMap<String, MsgBean>();
        // 遍历yml中配置好的报文字段
        for(Field field : fields ){
            // 命令代码
            if("commandCode".equals(field.getName())){
                commandCode = field.get(clazz).toString();
                // 通过命令代码，从全局报文配置变量中取出对应字段的Map集合
                msgBeanMap = ApplicationStartQuartzJobListener.chm.get(commandCode);
            }
            // 字段名称
            String fieldName = field.getName();
            // 字段值
            String fieldValue = field.get(clazz).toString();
            // 字段属性实体类
            MsgBean msgBean = msgBeanMap.get(fieldName);
            // 如果字段值为空，则从返回的报文中获取对应的字段值
            if(StringUtils.isBlank(fieldValue) && "time".equals(msgBean.getType())){
                fieldValue = sdf.format(new Date());
            }
            mapAll.put(fieldName, fieldValue);
        }
        listMap.put(commandCode, mapAll);
        if(StringUtils.isNoneBlank(chargePort)){
            MsgTypeConst.chm_Map.put(commandCode + "_" + chargePort, mapAll);
        }else {
            MsgTypeConst.chm_Map.put(commandCode, mapAll);
        }
        return listMap;
    }
}
